package com.adminpanel.service;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.sql.DataSource;


import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.adminpanel.modelbean.UserBean;
import com.adminpanel.util.Util;

public class UserServices {

	private JdbcTemplate jdbcTemplate;
	private DataSource dataSource;
	private JavaMailSender javaMailSender;
	
	
	@Resource(name = "dataSource")
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	@Resource
	public void setJavaMailSender(JavaMailSender javaMailSender) {
		this.javaMailSender = javaMailSender;
	}



	public void addUser(final UserBean userBean) {
		new DigestUtils();
		jdbcTemplate.update("insert into adminpanel values(?,?,?,?,?,?)",
				new PreparedStatementSetter() {

					@SuppressWarnings("static-access")
					@Override
					public void setValues(PreparedStatement ps) {
						
						try {
							ps.setInt(1, 0);
							ps.setString(2, userBean.getUsername());
							ps.setString(3, new DigestUtils().md5Hex(userBean.getPassword()));
							ps.setString(4, userBean.getEmail());
							ps.setInt(5, 0);
							ps.setString(6, new Util().getDateTime());
						} catch (SQLException e) {
							

							e.printStackTrace();
						}
					}

				});

	}
	
	
	
	
	public void updateStatus(String u,String token,String exp){
		String sql = "select username,password,email where username="+u;
		
	}
	
	
	public int findExistName(String username){
		String sqlStr = "select count(*) from adminpanel where username=?";
		return jdbcTemplate.queryForInt(sqlStr,new Object[]{username});
	}
	
	@SuppressWarnings("unchecked")
	public int findTheSame(final String data_field,final String data_value){
		String sql = "{call check1(?,?)}";
		Object obj = jdbcTemplate.execute(sql,new CallableStatementCallback() {

			@Override
			public Object doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				cs.setString(1, data_field);
				cs.setString(2, data_value);
				cs.execute();
				return new Integer(cs.getInt(1));
			}
		});
		return ((Integer)obj).intValue();
		
	}
	
	public int getMatchCount(String username, String password) {
		String sqlStr = "select count(*) from adminpanel where username=? and password=?";
		return jdbcTemplate.queryForInt(sqlStr,new Object[]{username,password});
	}
	
	public UserBean findUserByName(String username) {
		String sqlStr="select * from adminpanel where username=?";
		final UserBean userBean = new UserBean();
		jdbcTemplate.query(sqlStr,new Object[]{username},
			new RowCallbackHandler(){
			public void processRow(ResultSet rs)throws SQLException{
				userBean.setId(rs.getInt("id"));
				userBean.setUsername(rs.getString("username"));
				userBean.setPassword(rs.getString("password"));
				userBean.setEmail(rs.getString("email"));
				userBean.setStatus(rs.getInt("status"));
				userBean.setRegister_time(rs.getString("register_time"));
			}
		});
		return userBean;
	}
	
	public void updatepw(String pw,String username){
		
		String sql = "update adminpanel set password = ? where username = ?";
		
		jdbcTemplate.update(sql, new Object[] {pw, username});
	}
	
	public void sendlink(String usermail,String subject,String content){
		MimeMessage message = javaMailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message);
			helper.setFrom("11148509@qq.com");
			helper.setTo(usermail);
			helper.setSubject(subject);
			helper.setText(content);
			
			javaMailSender.send(message);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("static-access")
	public boolean checkUser(UserBean userBean) {
		new DigestUtils();
		String username = userBean.getUsername();
		String password = new DigestUtils().md5Hex(userBean.getPassword());
		int status = userBean.getStatus();
		//System.out.println(status);
		int matchCount = getMatchCount(username, password);
		if(matchCount == 0 || status ==0){
			return false;
		}
		else {
			return true;
		}
	}
	
	
}
